<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>轮播图</title>
    <style>
      /* 轮播容器样式 */
      .carousel-container {
        position: relative;
        width: 80%; /* 设置轮播容器宽度 */
        margin: auto; /* 居中显示 */
        overflow: hidden;
      }

      /* 轮播项样式 */
      .carousel-item {
        width: 100%; /* 设置轮播项宽度 */
        height: 300px; /* 设置轮播项高度 */
        background-size: cover; /* 图片尺寸自适应容器 */
        background-position: center; /* 图片居中显示 */
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 24px;
        color: #fff; /* 设置文本颜色 */
      }

      /* 上一页、下一页按钮样式 */
      .pagebutton {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        width: 50px; /* 设置按钮宽度 */
        height: 50px; /* 设置按钮高度 */
        background-color: #3498db; /* 设置按钮背景颜色 */
        color: #fff; /* 设置按钮文本颜色 */
        text-align: center;
        line-height: 50px;
        font-size: 16px;
        cursor: pointer;
      }

      .pagebutton:hover {
        background-color: #2980b9; /* 设置按钮鼠标悬停时的背景颜色 */
      }

      /* 上一页按钮位置 */
      .pagebutton:first-child {
        left: 0;
      }

      /* 下一页按钮位置 */
      .pagebutton:last-child {
        right: 0;
      }
      /* 背景图片路径 */
      #carouselItem {
        background-image: url("./images/photo1.jpg");
      }
      /* 小方块样式 */
      .page-indicator {
        display: flex;
        justify-content: center;
        margin-top: 10px;
      }

      .indicator-item {
        width: 10px;
        height: 10px;
        background-color: #3498db;
        margin: 0 5px;
        border-radius: 50%;
        cursor: pointer;
      }

      .indicator-item.active {
        background-color: #2980b9;
      }
    </style>
  </head>
  <body>
    <div class="carousel-container">
      <div class="pagebutton" onclick="prevPage()">上一页</div>

      <div class="carousel-item" id="carouselItem"></div>

      <div class="pagebutton" onclick="nextPage()">下一页</div>
    </div>
    <div class="page-indicator">
      <div class="indicator-item" onclick="goToPage(1)"></div>
      <div class="indicator-item" onclick="goToPage(2)"></div>
      <div class="indicator-item" onclick="goToPage(3)"></div>
      <!-- 添加更多小方块以匹配图片数量 -->
    </div>
    <script>
      let count = 1;

      const imgs = document.querySelector("#carouselItem");
      const indi = document.querySelectorAll(".indicator-item");

      console.log(imgs);

      function nextPage() {
        count = count >= 3 ? 1 : count + 1;
        // imgs.style.backgroundImage = `url(./images/photo${count}.jpg)`;

        goToPage(count);
      }
      function prevPage() {
        count = count <= 1 ? 3 : count - 1;
        // imgs.style.backgroundImage = `url(./images/photo${count}.jpg)`;

        goToPage(count);
      }

      function goToPage(num) {
        count = num;
        imgs.style.backgroundImage = `url(./images/photo${count}.jpg)`;
        changeIndi(count);
      }
      function changeIndi(num) {
        indi.forEach((item) => {
          item.style.backgroundColor = "#2980b9";
        });

        indi[num - 1].style.backgroundColor = "pink";
      }
    </script>
  </body>
</html>
